package com.migo.service.impl;

import com.migo.dao.ShOrderInfoDao;
import com.migo.dao.ShOrderServiceTypeDetailDao;
import com.migo.dao.ShOrderStatusLineDao;
import com.migo.dao.ShSparePartInfoDetailDao;
import com.migo.entity.ShOrderInfoEntity;
import com.migo.entity.ShOrderServiceTypeDetailEntity;
import com.migo.entity.ShOrderStatusLineEntity;
import com.migo.entity.ShSparePartInfoDetailEntity;
import com.migo.service.ShOrderInfoService;
import com.migo.view.ShOrderInfoView;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("shOrderInfoService")
public class ShOrderInfoServiceImpl implements ShOrderInfoService {
    @Autowired
    private ShOrderInfoDao shOrderInfoDao;

    @Autowired
    private ShOrderStatusLineDao shOrderStatusLineDao;

    @Autowired
    private ShOrderServiceTypeDetailDao shOrderServiceTypeDetailDao;

    @Autowired
    private ShSparePartInfoDetailDao shSparePartInfoDetailDao;

    @Override
    public ShOrderInfoEntity queryObject(Integer id) {
        return shOrderInfoDao.queryObject(id);
    }

    @Override
    public ShOrderInfoEntity queryOrderId(String orderId) {
        return shOrderInfoDao.queryOrderId(orderId);
    }

    @Override
    public List<ShOrderInfoEntity> queryList(Map<String, Object> map) {
        return shOrderInfoDao.queryList(map);
    }

    @Override
    public List<ShOrderInfoEntity> queryUserOrderList(Map<String, String> map) {
        return shOrderInfoDao.queryUserOrderList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return shOrderInfoDao.queryTotal(map);
    }

    @Override
    public void save(ShOrderInfoEntity shOrderInfo) {
        shOrderInfoDao.save(shOrderInfo);
    }

    @Override
    public void updateAddress(ShOrderInfoEntity shOrderInfo) {
        shOrderInfoDao.updateAddress(shOrderInfo);
    }
    @Override
    public void updateUserReceiving(ShOrderInfoEntity shOrderInfo) {
        shOrderInfoDao.updateUserReceiving(shOrderInfo);
    }

    @Override
    public void update(ShOrderInfoEntity shOrderInfo) {
        shOrderInfoDao.update(shOrderInfo);
    }

    @Override
    public void delete(Integer id) {
        shOrderInfoDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        shOrderInfoDao.deleteBatch(ids);
    }

    @Override
    public ShOrderInfoView queryObjectById(Integer id) {
        ShOrderInfoView orderInfoView = new ShOrderInfoView();
        ShOrderInfoEntity shOrderInfo = shOrderInfoDao.queryObject(id);
        BeanUtils.copyProperties(shOrderInfo, orderInfoView);
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderStatusId",shOrderInfo.getOrderStatusId());
        map.put("serviceType",shOrderInfo.getServiceTypeId());
        map.put("countryArea",shOrderInfo.getCountryArea());
        ShOrderStatusLineEntity orderStatusLineEntity = shOrderStatusLineDao.queryByIdAndLanguage(map);
        orderInfoView.setOrderStatusName(orderStatusLineEntity.getActionName());
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("orderId", id);
        List<ShOrderServiceTypeDetailEntity> serviceList = shOrderServiceTypeDetailDao.queryList(paramMap);
        orderInfoView.setOrderServiceDetailList(serviceList);
        List<ShSparePartInfoDetailEntity> spareList = shSparePartInfoDetailDao.queryList(paramMap);
        orderInfoView.setOrderSpareDetailList(spareList);
        orderInfoView.setImages(returnImages(shOrderInfo.getQuestionImagePath()));
        return orderInfoView;
    }

    public List<String> returnImages(String imagePath) {
        List<String> images = new ArrayList<>();
        if (!StringUtils.isEmpty(imagePath)) {
            String[] imageArr = imagePath.split(",");
            for (int i = 0; i < imageArr.length; i++) {
                String image = "http://gcsbucket.oss-cn-hongkong.aliyuncs.com" + imageArr[i];
                images.add(image);
            }
        }
        return images;
    }

}
